package com.ruoyi.common.utils;

import com.alibaba.fastjson.JSON;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class JsVarParser {

    public static List<String> parseChatShow(String htmlContent) {
        List<String> result = new ArrayList<>();

        // 提取 var st
        Pattern patternSt = Pattern.compile("var\\s+st\\s*=([^;]+);", Pattern.DOTALL);
        Matcher matcherSt = patternSt.matcher(htmlContent);
        List<Integer> stList = null;
        if (matcherSt.find()) {
            String stJson = matcherSt.group(1).trim().replace("'", "\"");
            try {
                stList = JSON.parseArray(stJson, Integer.class);
                System.out.println("✅ st 解析成功，长度：" + stList.size());
            } catch (Exception e) {
                System.err.println("❌ st JSON 解析失败: " + stJson);
            }
        } else {
            System.err.println("❌ 没有找到 var st");
        }

        // 提取 var stChartValue
        Pattern patternStChartValue = Pattern.compile("var\\s+stChartValue\\s*=[\\s\\S]*?(?=;|</script>)", Pattern.DOTALL);
        Matcher matcherStChartValue = patternStChartValue.matcher(htmlContent);
        List<String> stChartValueList = null;
        if (matcherStChartValue.find()) {
            String matchContent = matcherStChartValue.group(0);
            String arrayContent = matchContent.replaceFirst(".*?\\s*=\\s*", "").trim();
            String stChartValueJson = arrayContent.replace("'", "\"");

            // 去除注释和其他 JS 代码干扰
            stChartValueJson = stChartValueJson.replaceAll("(//.*)|(?i)(/\\*.*?\\*/)", "").trim();

            // 提取合法 JSON 数组部分
            Pattern jsonArrayPattern = Pattern.compile("(\\[.*?\\])", Pattern.DOTALL);
            Matcher jsonArrayMatcher = jsonArrayPattern.matcher(stChartValueJson);
            if (jsonArrayMatcher.find()) {
                stChartValueJson = jsonArrayMatcher.group(1);
                try {
                    stChartValueList = JSON.parseArray(stChartValueJson, String.class);
                    System.out.println("✅ stChartValue 解析成功，长度：" + stChartValueList.size());
                } catch (Exception e) {
                    System.err.println("❌ stChartValue JSON 解析失败: " + stChartValueJson);
                }
            } else {
                System.err.println("❌ 未找到有效的 JSON 数组");
            }
        } else {
            System.err.println("❌ 没有找到 var stChartValue");
        }

        // 合并数据
        if (stList != null && stChartValueList != null && stList.size() == stChartValueList.size()) {
            for (int i = 0; i < stList.size(); i++) {
                String key = stChartValueList.get(i).replaceAll("\\s+", "").replace("<br>", "");
                int value = stList.get(i);
                if (!key.contains("四球")) {
                    result.add(key + "\t" + value);
                }
            }
        } else {
            System.err.println("❌ 数组长度不一致：st=" + (stList != null ? stList.size() : 0)
                    + ", stChartValue=" + (stChartValueList != null ? stChartValueList.size() : 0));
        }

        return result;
    }
}
